//
//  HexMapGridLayer.cpp
//  DiceWar
//
//  Created by xiaofeng on 19/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include "HexMapGridLayer.h"
#include "GameConstants.h"
#include "StringUtil.h"

USING_NS_CC;

namespace  
{
    const char* kRedCellSpritePath = "red.png";
    const char* kGrayCellSpritePath = "gray.png";
    const char* kBlueCellSpritePath = "blue.png";
    const char* kOrangeCellSpritePath = "orange.png";
    const char* kBlackCellSpritePath = "black.png";
    
    const char* kDiceNumDefaultContent = " ";
    const char* kDiceNumFont = "Arial";
    const int kDiceNumFontSize = 20;
}

HexMapGridLayer::HexMapGridLayer()
: m_pCellSprite(NULL)
, m_pNumDicesLabel(NULL){}

HexMapGridLayer::~HexMapGridLayer() {}

bool HexMapGridLayer::init()
{
    if (!CCLayer::init()) return false;
    return true;
}

void HexMapGridLayer::setDisplay(const HexMapGrid& mapGrid)
{
    HexMapGrid::GridType gridType = mapGrid.getType();
    if (gridType != HexMapGrid::kFight)
    {
        hide();
        return;
    }
    
    setCellSprite(mapGrid);
    setNumDicesSprite(mapGrid);
}

void HexMapGridLayer::hide()
{
    if (m_pCellSprite) m_pCellSprite->setIsVisible(false);
    if (m_pNumDicesLabel) m_pNumDicesLabel->setIsVisible(false);
}

void HexMapGridLayer::setCellSprite(const HexMapGrid& mapGrid)
{
    if (m_pCellSprite) removeChild(m_pCellSprite, true);
    
    const char* cellSpritePath = getCellSpritePath(mapGrid.getPlayerType());
    m_pCellSprite = cocos2d::CCSprite::spriteWithFile(cellSpritePath);
    CCAssert(m_pCellSprite, "Failed to create sprite");
    if (!m_pCellSprite) return;
    
    const CCSize kSize = m_pCellSprite->getContentSize();
    const int kTextureWidth = kSize.width;
    const float kScale = static_cast<float>(gameConstants::kHexGridSize * 2) / static_cast<float>(kTextureWidth);
    m_pCellSprite->setScale(kScale);
    
    const CCPoint kPosition = mapGrid.getPosition();
    m_pCellSprite->setPosition(kPosition);
    
    addChild(m_pCellSprite, 0);
}

void HexMapGridLayer::setNumDicesSprite(const HexMapGrid& mapGrid)
{
    const int kNumDices = mapGrid.getNumDices();
    std::string labelContent = StringUtil::ToString(kNumDices);
    
    if (!m_pNumDicesLabel)
    {
        m_pNumDicesLabel = CCLabelTTF::labelWithString(kDiceNumDefaultContent, CCSize(15.0f, 15.0f), CCTextAlignmentCenter, kDiceNumFont, kDiceNumFontSize);    
        const CCPoint kPosition = mapGrid.getPosition();
        m_pNumDicesLabel->setPosition(kPosition);
        addChild(m_pNumDicesLabel, 1);
    }
    
    m_pNumDicesLabel->setIsVisible(true);
    m_pNumDicesLabel->setString(labelContent.c_str());
}

const char* HexMapGridLayer::getCellSpritePath(PlayerType playerType)
{
    switch (playerType) 
    {
        case kRedPlayer: return kRedCellSpritePath;
        case kBluePlayer: return kBlueCellSpritePath;
        case kBlackPlayer: return kBlackCellSpritePath;
        case kOrangePlayer: return kOrangeCellSpritePath;
        case kInvalidPlayer: return kGrayCellSpritePath;
        default: break;
    }
    
    return kGrayCellSpritePath;
}